//
//  HWGameLiveMyFollowController.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/1/19.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit

class HWGameLiveMyFollowController: HWBaseViewController {
    var collectionView:UICollectionView!
    let collectionProtocol = HWGameLiveMyFollowProtocol()
    var httpManager = HWGameLiveHttpManager()
    let mutDataArr = NSMutableArray()
    override func viewDidLoad() {
        super.viewDidLoad()

        initUI()
        
        initFrame()
        
        addRefresh()
        
        addAction()
        
        loadData()
    }
    
}
extension HWGameLiveMyFollowController {
    private func initUI() -> Void {
        navigationView.isHidden = false
        navigationView.titleLabel.text = "我的关注"
        navigationView.backButton.setImage(UIImage(named: "fanhui"), for: .normal)
        view.backgroundColor = .colorWithHexString(hexString: "#F7F7F7")
        
        let flowL = UICollectionViewFlowLayout.init()
        flowL.minimumLineSpacing = 7*widthScale
        flowL.minimumInteritemSpacing = 7*widthScale
        flowL.scrollDirection = .vertical
        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: flowL)
        collectionView.delegate = collectionProtocol
        collectionView.dataSource = collectionProtocol
        collectionView.backgroundColor = .white
        collectionView.showsVerticalScrollIndicator = false
        collectionView.register(HWGameLiveCell.self, forCellWithReuseIdentifier: "HWGameLiveCell")
        view.addSubview(collectionView)
        collectionView.ly_emptyView = LYEmptyView.empty(withImageStr: "", titleStr: "", detailStr: "暂无数据")
        collectionView.ly_emptyView.contentViewOffset = -40
    }
    
    private func initFrame() -> Void {
        collectionView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(navigationView.snp.bottom)
        }
    }
}


extension HWGameLiveMyFollowController {
    func addRefresh() -> () {
        
        collectionView.mj_header = MJRefreshNormalHeader()
        collectionView.mj_header?.setRefreshingTarget(self, refreshingAction: #selector(self.loadNewData))
//        collectionView.mj_footer = MJRefreshBackNormalFooter()
//        collectionView.mj_footer?.setRefreshingTarget(self, refreshingAction: #selector(self.loadMoreData))
    }
    
    
    @objc func loadNewData() -> () {
        loadData()
    }
    
//    @objc func loadMoreData() -> () {
//
//        self.httpManager.getGameLiveRoomsFooter(para: ["type":"tagId"]) { modelArr in
//            self.collectionView.mj_footer?.endRefreshing()
//            if modelArr.count > 0 {
//                self.mutDataArr.addObjects(from: modelArr)
//            }
//            self.collectionProtocol.models = self.mutDataArr as? [HWGameLiveListModel]
//            self.collectionView.reloadData()
//        } failure: { error in
//            if error == "暂无房间"{
//                self.collectionView.mj_footer?.endRefreshingWithNoMoreData()
//            }else{
//                self.collectionView.mj_footer?.endRefreshing()
//            }
//        }
//
//    }
    
    func loadData() -> Void {
        httpManager.getGameLiveMyFollow() { models in
            
            self.collectionView.mj_header?.endRefreshing()
            self.collectionView.mj_footer?.endRefreshing()
            if models.count > 0 {
                self.mutDataArr.removeAllObjects()
                self.mutDataArr.addObjects(from: models)
            }
            self.collectionProtocol.models = self.mutDataArr as? [HWGameLiveListModel]
            self.collectionView.reloadData()
        } failure: { error in
            self.collectionView.mj_header?.endRefreshing()
            self.collectionView.mj_footer?.endRefreshing()
        }
        
    }
}

extension HWGameLiveMyFollowController {
    func addAction() -> Void {
        collectionProtocol.jumpToLiveRoom = {
            model in
            if model.status == "0" {
                self.view.showHUD(title: "暂未开播")
                return
            }
            if HWLocallyStoredData.getLoginUserID().count>0{
                
                //                let liveRoom = HWGameLiveRoomController()
                //                self.navigationController?.pushViewController(liveRoom, animated: true)
                self.httpManager.getGameLiveRoomInfo(roomId: model.room_id) { roomInfo in
                    
                    V2TIMManager.sharedInstance().getGroupsInfo([model.room_id]) { [weak self] groupInfos in
                        guard self != nil else { return }
                        guard let groupInfo = groupInfos?.first else { return }
                        if groupInfo.resultCode == 0 {
                            HWGameLiveImManager.shared.enterRoom(roomId: model.room_id) { code, message in
                                print(message)
                            }
                            let gameLiveRoomController = HWGameLiveRoomController()
                            roomInfo.ownerId = groupInfo.info.owner
                            gameLiveRoomController.liveModel = roomInfo
                            gameLiveRoomController.closeCallBack = {
                                self?.loadData()
                            }
                            self?.navigationController?.pushViewController(gameLiveRoomController, animated: true)
                            
                        }
                        
                    } fail: { code, message in
                        print(message ?? "")
                    }
                    
                } failure: { error in
                    print(error)
                }
            }else{
                let loginController = HWLoginController()
                self.navigationController?.pushViewController(loginController, animated: true)
            }
        }
    }
    
}
